home *** CD-ROM | disk | FTP | other *** search
- Path: news.ssnet.com!not-for-mail
- From: helie@ssnet.com (Ray Helie)
- Newsgroups: comp.lang.c++
- Subject: Re: Virtual Members: Difficult Question
- Date: 19 Jan 1996 14:53:39 -0500
- Organization: SSNet, Inc.
- Message-ID: <4doss3$b1i@marlin.ssnet.com>
- References: <4dmha6$80c@marlin.ssnet.com> <4doof9$hom@charnel.ecst.csuchico.edu>
- NNTP-Posting-Host: marlin.ssnet.com
-
- James Robert McElroy <mcelroy@ecst.csuchico.edu> wrote:
- - A general comment about your code:
- -
- - You are casting the wrong way in the inheritance hierarchy.
- - This is guaranteed to blow up at some point, often unpredic-
- - tably. Some times it will work, some times it won't (similar
- - to driving the wrong way down a one-way street).
- -
- - Look at it this way: in terms of physical space taken up by
- - a class object, a base class object will always take up less room than
- - a derived class object, right? Now, if you tell a base class object it is
- - actually a derived class object (via the cast) the pointer now thinks
- - it has more room in memory than it actually does.
-
- Unless the base class object was *itself* gotten by casting it
- from the larger derived object, which is what *I've* done..
- I understand what you're saying, but when the memory at that
- object's location was originally allocated, it /was/ of the
- larger size (the derived object's size)-- look at the sample
- code again, I think I stated it correctly.
-
-
- - Look at it this way -- would the following code cause problems?
- -
- - int foo;
- - long * fubar = (long*)&foo;
- - *fubar = 10; // gee, no problem here
- - *fubar = 32468551; // uh oh. Seems to crash. Don't know why!
- -
- - Yup. Similar to what you are experiencing.
-
-
- You are right and I'm well aware of /that/ problem (who isn't?)-- your
- example is not a match of my example. Using what you just wrote, /this/
- is my problem (although not using objects, this example seems pointless):
-
- int foo;
- long foobar;
- int* pInt;
- long* pLong
-
- foobar = 1234567;
- pInt = &foobar; // pointless, but it will not stop you
- // right now I have a pointer to a smaller object (pInt), but the
- // larger object's data (pLong) should still be there,
- // so the following line:
- pLong = pInt;
- // followed by
- printf ("%l",*pLine); // should print out 1234567
- // should still work. and that's what I'm doing in my program.
- // the memory for the biggest variable (ie., the derived class)
- // is already allocated, even though later I cast the pointer
- // to a smaller object (the base class), and it thinks there's
- // less memory there. but there *is* more memory there
- // due to the *initial* definition.
-
- // and now what fails for me is:
- char* pChar;
- pChar = pInt; // again pointless
- pLong = pChar;
- *pLong = 11223344;
- printf ("%l",*pLine); // this line fails, trying to show
- // similar problem I'm having.. it should have printed out
- // 11223344, but instead dies.
-
- In a nutshell, I declared object of derived size, cast to the
- base, cast again to a parent class of base, cast back to base,
- tried to write a derived amount of data to the base class (as if
- it was an array of characters, starting at the object's address,
- which is the same way I wrote the information to begin with), and
- it dies on me when I do a virtual function call. The derived amount of
- space is there -- that's what I started with.
-
- So I don't think you understand my question, because I understand
- what you're saying, you are right under the circumstances you
- described, but I think you got the circumstances wrong...?
-
-
- If anyone else can help, I'd appreciate it. :)
-